/**
 * 
 */
package fr.isima.isipaint.model;

import java.awt.BasicStroke;
import java.awt.Color;

import javax.swing.JLayeredPane;
import javax.swing.TransferHandler;

/**
 * @author daniel
 * 
 *         Represent an abstract object to display
 * 
 */
public abstract class GraphicObject extends JLayeredPane implements Cloneable {

	/** The number of graphic object created */
	private static int CPT_GRAPHIC_OBJECTS = 0;

	/** Default width */
	protected static final int DEFAUL_WIDTH = 2;

	/** Default height */
	protected static final int DEFAUL_HEIGHT = 2;

	/** The brush to draw */
	protected BasicStroke stroke = new BasicStroke();

	/** The object's id */
	private int id;

	/** The color of the object (default is black) */
	protected Color color = Color.BLACK;

	/** The object is filled ? */
	protected boolean filled;

	/**
	 * @return the id
	 */
	public final int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public final void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the color
	 */
	public final Color getColor() {
		return color;
	}

	/**
	 * @param color
	 *            the color to set
	 */
	public final void setColor(Color color) {
		this.color = color;
	}

	/**
	 * @return the filled
	 */
	public final boolean isFilled() {
		return filled;
	}

	/**
	 * @param filled
	 *            the filled to set
	 */
	public final void setFilled(boolean filled) {
		this.filled = filled;
	}

	/**
	 * The default constructor
	 */
	protected GraphicObject() {
		setBackground(Color.WHITE);
		id = CPT_GRAPHIC_OBJECTS++;
		setBounds(0, 0, DEFAUL_WIDTH, DEFAUL_HEIGHT);
		setLayout(null);
		isOptimizedDrawingEnabled();
		setOpaque(false);
	}

	/**
	 * 
	 * @param x
	 *            The x coordinate of the object
	 * @param y
	 *            The y coordinate of the object
	 * @param width
	 *            The width of the object
	 * @param height
	 *            The height of the object
	 */
	public GraphicObject(int x, int y, int width, int height) {
		this();
		setBounds(x, y, width, height);
		isOptimizedDrawingEnabled();
	}

	/**
	 * @param id
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 */
	public GraphicObject(int id, int x, int y, int width, int height) {
		setId(id);
		setBounds(x, y, width, height);
		isOptimizedDrawingEnabled();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#clone()
	 */
	@Override
	public Object clone() {
		GraphicObject object = null;

		try {
			object = (GraphicObject) super.clone();
			object.setId(CPT_GRAPHIC_OBJECTS++);
		} catch (CloneNotSupportedException e) {
			object = null;
			e.printStackTrace();
		}

		return object;
	}

	/* ======================================================= */
	/**
	 * @see java.lang.Object#toString()
	 */
	/* ======================================================= */
	@Override
	public String toString() {
		return "GraphicObject  [ id=" + id + ", x=" + getX() + ", y=" + getY()
				+ " width= " + getWidth() + ", height= " + getHeight()
				+ ",color=" + color + ", filled=" + filled + ", visible="
				+ isVisible() + "]";
	}

}
